﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-25 11:00:45
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class FormFieldRela:IFormFieldRela
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.CGTConnectionString;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(RelaID),0)+1
                               FROM [FormFieldRela]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private FormFieldRelaInfo GetByReader(IDataReader dr)
         {
            FormFieldRelaInfo info = new FormFieldRelaInfo();
            object obj;
            obj=dr["RelaID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.RelaID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["BizTypeCityRelaID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeCityRelaID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["FormFieldID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.FormFieldID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["FieldSort"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.FieldSort = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["UpdatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["CreatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedUserID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["UpdatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdatedUserID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            return info;
         }
		public int Insert(FormFieldRelaInfo info)
		{
        
        
            string strSql=@"INSERT INTO FormFieldRela			
			(			
			[BizTypeCityRelaID],
			[FormFieldID],
			[FieldSort],
			[CreatedDate],
			[UpdatedDate],
			[CreatedUserID],
			[UpdatedUserID],
			[IsActive]
			)
			 VALUES
			(			
			@BizTypeCityRelaID,
			@FormFieldID,
			@FieldSort,
			@CreatedDate,
			@UpdatedDate,
			@CreatedUserID,
			@UpdatedUserID,
			@IsActive
			)
			 SET @RelaID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@RelaID",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@BizTypeCityRelaID",DbType.Int32, info.BizTypeCityRelaID);
            db.AddInParameter(dbc, "@FormFieldID",DbType.Int32, info.FormFieldID);
            db.AddInParameter(dbc, "@FieldSort",DbType.Int32, info.FieldSort);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID",DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID",DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"RelaID");
         
		}
		
		
        public void Update(FormFieldRelaInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE FormFieldRela SET ");
            sbSql.AppendLine("[BizTypeCityRelaID] = @BizTypeCityRelaID,");
                
            sbSql.AppendLine("[FormFieldID] = @FormFieldID,");
                
            sbSql.AppendLine("[FieldSort] = @FieldSort,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[UpdatedDate] = @UpdatedDate,");
                
            sbSql.AppendLine("[CreatedUserID] = @CreatedUserID,");
                
            sbSql.AppendLine("[UpdatedUserID] = @UpdatedUserID,");
                
            sbSql.AppendLine("[IsActive] = @IsActive");
                
            sbSql.Append(" where  [RelaID] = @RelaID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@RelaID",DbType.Int32, info.RelaID);
            db.AddInParameter(dbc, "@BizTypeCityRelaID",DbType.Int32, info.BizTypeCityRelaID);
            db.AddInParameter(dbc, "@FormFieldID",DbType.Int32, info.FormFieldID);
            db.AddInParameter(dbc, "@FieldSort",DbType.Int32, info.FieldSort);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID",DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID",DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int relaID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [FormFieldRela]  ");
            strSql.Append("WHERE [RelaID]=@RelaID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@RelaID",DbType.Int32, relaID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<FormFieldRelaInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<FormFieldRelaInfo> list = new List<FormFieldRelaInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "FormFieldRela");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    FormFieldRelaInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public FormFieldRelaInfo GetByID(int relaID)
		{
            Model.FormFieldRelaInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [FormFieldRela] ");
            strSql.Append("WHERE [RelaID]=@RelaID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@RelaID",DbType.Int32, relaID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}